clear
clear matrix
set more off

* ------------------------------------------------------------------------------
* set paths
global root "[include file path here]"
global input "$root/Input"
global output "$root/Output"
global intermediate "$root/Intermediate"
global figures "$root/Figures"

* ------------------------------------------------------------------------------
* Create collapsed sample for average water use over time
* ------------------------------------------------------------------------------

* FULL SAMPLE
use "$input/eventStudyPanel_raw.dta", clear
keep if conversions == 0 | (conversions == 1 & wg == "SF")
collapse (mean) wuse (count) pid=wuse, by(year enroll2) fast
save "$intermediate/eventStudyPanel_full_collapse.dta", replace

* MATCHED SAMPLE
use "$input/eventStudyPanel_matched.dta", clear
collapse (mean) wuse (count) pid=wuse, by(year enroll2) fast
save "$intermediate/eventStudyPanel_matched_collapse.dta", replace

* ------------------------------------------------------------------------------
* Underlying data for summary plot of average water use over time (de-trended)
* ------------------------------------------------------------------------------

* FULL SAMPLE
use "$input/eventStudyPanel_raw.dta", clear
keep if conversions == 0 | (conversions == 1 & wg == "SF")
tab enroll2, m
keep wuse pid month year cohort2 enroll2
reghdfe wuse if enroll2 != 2, ///
a(i.pid#i.month i.year#i.month#i.cohort2) pool(1) vce(cl pid) resid
predict double wuse_dt if e(sample), residuals
drop _reghdfe_resid
collapse (mean) wuse_dt (count) pid=wuse, by(year enroll2) fast
save "$intermediate/eventStudyResiduals_full_collapse.dta", replace

* MATCHED SAMPLE
use "$input/eventStudyPanel_matched.dta", clear
tab enroll2, m
keep wuse pid month year cohort2 enroll2 parcelfreq
reghdfe wuse if enroll2 != 2 [fweight=parcelfreq], ///
a(i.pid#i.month i.year#i.month) pool(1) vce(cl pid) resid
predict double wuse_dt if e(sample), residuals
drop _reghdfe_resid
collapse (mean) wuse_dt (count) pid=wuse, by(year enroll2) fast
save "$intermediate/eventStudyResiduals_matched_collapse.dta", replace


local sample_id "full matched"
foreach i of local sample_id {

* ------------------------------------------------------------------------------
* Summary plot of average water use over time
* ------------------------------------------------------------------------------
use "$intermediate/eventStudyPanel_`i'_collapse.dta", clear

sort enroll2 year
gen wuse_non = wuse if enroll2 == 0
gen wuse_pre = wuse if enroll2 == 1
gen wuse_post = wuse if enroll2 == 2

local ti1 "Historical average water use"
local ti2 "single family participants (1 conversion) & non-participants"
local yti "average water use [1000 gal]"
local ll1 "participating parcels pre-enrollment"
local ll3 "participating parcels post-enrollment"
local ll5 "non-participating parcels"

gr tw sc wuse_pre wuse_non year if year <= 2013, m(i i) c(l l) ///
ytitle("`yti'") xtitle("") ylabel(, nogrid) graphregion(color(white)) ///
xsize(8) ysize(5) xla(1988(2)2013) legend(label(1 "`ll1'") label(2 "`ll5'") ///
order(1 2) col(2) region(color(white))) xline(1998, lcolor(black)) ///
lpattern("-" "l") lc(gs0 gs10) lw(thin medium) 
graph export "$figures/figure_2`i'.pdf", replace

* ------------------------------------------------------------------------------
* De-trended water use
* ------------------------------------------------------------------------------
use "$intermediate/eventStudyResiduals_`i'_collapse.dta", clear
reshape wide wuse_dt pid, i(year) j(enroll2)
local ll1 "participating parcels pre-enrollment"
local ll5 "non-participating parcels"
gr tw sc wuse_dt1 wuse_dt0 year if year <= 2013, m(i i) c(l l) xsize(8) ///
ytitle("average residual water use [1000 gal]") xtitle("") lc(gs0 gs10) ///
ylab(-3(1)3, nogrid) graphregion(color(white)) ysize(5) xla(1988(2)2013) ///
legend(label(1 "`ll1'") label(2 "`ll5'") order(1 2) region(color(white)) ///
col(2)) xline(1998, lcolor(black)) lpattern("-" "l") lw(thin medium)
graph export "$figures/avgWtrUse_residual_`i'.pdf", replace
}